﻿/*-----------------------------------------------------------------------------!
! SaleTypeKazImpl class                                                        !
!------------------------------------------------------------------------------!
! System   : Inventory Management System                                       !
! Client   : UK based                                                          !
! Date     : April 2011                                                        !
! Copyright: (C) KAWISOFT Inc                                                  !
! Program  : SaleTypeKazImpl.cs                                                !
! Function : Implementation of ISaleTypeKaz interface                          !
!------------------------------------------------------------------------------!
! Notes:                                                                       !
! Original author: Md. Shaiful Islam                                           !
! Last changed by: Md. Shaiful Islam, May 09 2011                              !
===============================================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Sadharon;
using Model.Upadan;
using System.Data.SqlClient;
using Model.Sohaok;

namespace Model.Poribar.SaleTypePanjeri
{
    class SaleTypeKazImpl : ISaleTypeKaz
    {
        #region SaleTypeKazImpl Members: Add Method
        /**
         * add : added new saleType
         * @param object
         * @return Result object
         **/
        public Result add(object _obj)
        {
            Result result = new Result();
            SaleType saleType = new SaleType();
            DBSadharon dbUtil = new DBSadharon();
            saleType = (SaleType)_obj;

            SqlConnection conn = new SqlConnection();
            try
            {
                conn = DatabaseConnection.GetConnection();
                // get UniqId for saleType
                string uniqId = dbUtil.getUniqId(conn, "SaleType", "ST");
                if (uniqId.Equals("falied"))
                {
                    string msg = "Failed to add new saleType: Error in generating uniqeId. If problem persist " +
                                     "please contact with system administrator";
                    result = Util.setSuccessResult(false, msg);
                }
                else
                {
                    saleType.Id = uniqId;
                    conn.Open();
                    string query = "Insert into SaleType (Id, Name, " +
                        "Live) Values(" +
                        "'" + Util.filterString(saleType.Id) + "'," +
                        "'" + Util.filterString(saleType.Name) + "'," +
                        "'" + "L" + "'" +
                        ")";

                    if (DBSadharon.MakeQuery(query, conn).ExecuteNonQuery() > 0)
                    {
                        result = Util.setSuccessResult(true, "New sale type added successfully");
                    }
                    else
                    {
                        result = Util.setSuccessResult(false, "Failed to add (Query failed)");
                    }
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to add: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to add: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }

        #endregion

        #region SaleTypeKazImpl member: getById
        ///
        // getById
        // @param personId
        // @return result object
        ///
        public Result getById(string id)
        {
            Result result = new Result();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();
                string query = "Select * from SaleType Where Id='" + id + "' and Live='L'";
                SqlDataReader saleTypeDataReader = DBSadharon.MakeQuery(query, conn).ExecuteReader();
                if (saleTypeDataReader.HasRows)
                {
                    SaleType saleType = new SaleType();
                    while (saleTypeDataReader.Read())
                    {
                        saleType.Id = saleTypeDataReader["Id"].ToString();
                        saleType.Name = saleTypeDataReader["Name"].ToString();
                        saleType.Live = saleTypeDataReader["Live"].ToString();                        
                    }
                    result = Util.setSuccessResult(true, "Successful", saleType);
                }

            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to get sale type details: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to get sale type details: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region SaleTypeKazImpl member: getList
        ///
        // getList
        ///
        public Result getList()
        {
            Result result = new Result();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();
                string query = "Select * from SaleType Where Live='L'";
                SqlDataReader slaeTypeDataReader = DBSadharon.MakeQuery(query, conn).ExecuteReader();
                if (slaeTypeDataReader.HasRows)
                {
                    List<SaleType> listSaleType = new List<SaleType>();
                    SaleType saleType = new SaleType();
                    while (slaeTypeDataReader.Read())
                    {
                        saleType = new SaleType();
                        saleType.Id = slaeTypeDataReader["Id"].ToString();
                        saleType.Name = slaeTypeDataReader["Name"].ToString();
                        saleType.Live = slaeTypeDataReader["Live"].ToString();
                        listSaleType.Add(saleType);
                    }
                    result = Util.setSuccessResult(true, "Successful", listSaleType);
                }

            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to get sale type list: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to get sale type list: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region SaleTypeKazImpl Members: Update Method
        /**
         * update : update sale type information
         * @param object
         * @return Result object
         **/
        public Result update(object _obj)
        {
            Result result = new Result();
            SaleType saleType = new SaleType();
            saleType = (SaleType)_obj;
            saleType.Live = "L";

            SqlConnection conn = new SqlConnection();
            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();
                string query = "Update SaleType set " +
                    "Name='" + Util.filterString(saleType.Name) + "', " +
                    "Live='" + Util.filterString(saleType.Live) + "' " +
                    "Where Id='" + saleType.Id + "'";

                if (DBSadharon.MakeQuery(query, conn).ExecuteNonQuery() > 0)
                {
                    result = Util.setSuccessResult(true, "Sale type updated successfully");
                }
                else
                {
                    result = Util.setSuccessResult(false, "Failed to update sale type (Query failed)");
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to update sale type: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to update sale type: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion

        #region SaleTypeKazImpl Members: delete Method
        /**
         * delete : delete sale type information
         * @param id
         * @return Result object
         **/
        public Result delete(string id)
        {
            Result result = new Result();
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = DatabaseConnection.GetConnection();
                conn.Open();
                string query = "Update SaleType set Live='D' Where Id='" + id + "'";

                if (DBSadharon.MakeQuery(query, conn).ExecuteNonQuery() > 0)
                {
                    result = Util.setSuccessResult(true, "sale type deleted successfully");
                }
                else
                {
                    result = Util.setSuccessResult(false, "Failed to delete sale type (Query failed)");
                }
            }
            catch (SqlException ex)
            {
                result = Util.setSuccessResult(false, "Failed to delete sale type: " + ex.Message);
            }
            catch (Exception ex)
            {
                result = Util.setSuccessResult(false, "Failed to delete sale type: " + ex.Message);
            }
            finally
            {
                DBSadharon.closeConnection(conn);
            }
            return result;
        }
        #endregion
    }
}
